/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
define(["require", "exports", "./utils/commandManager", "./typeScriptServiceClientHost", "./commands", "./utils/plugins", "./utils/projectStatus", "./utils/languageModeIds", "./utils/languageConfigurations", "./utils/languageDescription", "./utils/managedFileContext", "./utils/lazy", "./utils/fileSchemes"], function (require, exports, commandManager_1, typeScriptServiceClientHost_1, commands, plugins_1, ProjectStatus, languageModeIds, languageConfigurations, languageDescription_1, managedFileContext_1, lazy_1, fileSchemes) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var vscode;
    function activate(context) {
        vscode = window["getVsCode"]("javascript");
        const plugins = plugins_1.getContributedTypeScriptServerPlugins(vscode);
        const commandManager = new commandManager_1.CommandManager(vscode);
        context.subscriptions.push(commandManager);
        const lazyClientHost = createLazyClientHost(context, plugins, commandManager);
        registerCommands(commandManager, lazyClientHost);
        //context.subscriptions.push(new TypeScriptTaskProviderManager(lazyClientHost.map(x => x.serviceClient)));
        context.subscriptions.push(vscode.languages.setLanguageConfiguration(languageModeIds.jsxTags, languageConfigurations.jsxTags));
        const supportedLanguage = [].concat.apply([], languageDescription_1.standardLanguageDescriptions.map(x => x.modeIds).concat(plugins.map(x => x.languages)));
        function didOpenTextDocument(textDocument) {
            if (isSupportedDocument(supportedLanguage, textDocument)) {
                openListener.dispose();
                // Force activation
                // tslint:disable-next-line:no-unused-expression
                void lazyClientHost.value;
                context.subscriptions.push(new managedFileContext_1.default(resource => {
                    return lazyClientHost.value.serviceClient.normalizePath(resource);
                }, vscode));
                return true;
            }
            return false;
        }
        const openListener = vscode.workspace.onDidOpenTextDocument(didOpenTextDocument, undefined, context.subscriptions);
        for (const textDocument of vscode.workspace.textDocuments) {
            if (didOpenTextDocument(textDocument)) {
                break;
            }
        }
    }
    exports.activate = activate;
    function createLazyClientHost(context, plugins, commandManager) {
        return lazy_1.lazy(() => {
            //const logDirectoryProvider = new LogDirectoryProvider(context);
            const clientHost = new typeScriptServiceClientHost_1.default(languageDescription_1.standardLanguageDescriptions, plugins, commandManager, vscode);
            context.subscriptions.push(clientHost);
            clientHost.serviceClient.onReady(() => {
                context.subscriptions.push(ProjectStatus.create(clientHost.serviceClient, (path) => new Promise(resolve => setTimeout(() => resolve(clientHost.handles(path)), 750)), context.workspaceState, vscode));
            });
            return clientHost;
        });
    }
    function registerCommands(commandManager, lazyClientHost) {
        commandManager.register(new commands.ReloadTypeScriptProjectsCommand(lazyClientHost));
        commandManager.register(new commands.ReloadJavaScriptProjectsCommand(lazyClientHost));
        // commandManager.register(new commands.SelectTypeScriptVersionCommand(lazyClientHost));
        // commandManager.register(new commands.OpenTsServerLogCommand(lazyClientHost));
        // commandManager.register(new commands.RestartTsServerCommand(lazyClientHost));
        // commandManager.register(new commands.TypeScriptGoToProjectConfigCommand(lazyClientHost));
        // commandManager.register(new commands.JavaScriptGoToProjectConfigCommand(lazyClientHost));
    }
    function isSupportedDocument(supportedLanguage, document) {
        if (supportedLanguage.indexOf(document.languageId) < 0) {
            return false;
        }
        return fileSchemes.isSupportedScheme(document.uri.scheme);
    }
});
